package nuc.soft.xsf.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import nuc.soft.xsf.model.Titletype;
import nuc.soft.xsf.service.ITitletypeService;
import nuc.soft.xsf.tools.Tools;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/TitleTypeController")
public class TitleTypeController {
   @Resource
   private ITitletypeService titleTypeService;
   
   
   @RequestMapping(value = "/add.action", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addTitletype(Titletype titleType) {
		titleType.setDeletestate(0);
		titleTypeService.insert(titleType);
		JSONObject json = new JSONObject();
		json.put("data", titleType);
		return json.toString();

	}

	@RequestMapping(value = "/delete.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String deleteTitletype(String ids) {
		String[] str = ids.split(",");
		JSONObject json = new JSONObject();
		int success = 0;
		for (String strTmp : str) {
			Titletype titleType = titleTypeService.findById(Integer.parseInt(strTmp));
			if (titleType != null) {
				titleType.setDeletestate(1);
				titleTypeService.update(titleType);
				success++;
			}
		}
		json.put("success", success);
		return json.toString();
	}

	@RequestMapping(value = "/update.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String updateTitletype(Titletype titleType) {
		titleTypeService.update(titleType);
		System.out.println(titleType.toString());
		JSONObject json = new JSONObject();
		Titletype titleType1 = titleTypeService.findById(titleType.getTitleTypeid());
		json.put("data", titleType1);
		json.put("redata", titleType.getTitleTypeid());
		json.put("error_code", 200);
		return json.toString();
	}

	@RequestMapping(value = "/findByCondition.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String findByCondition(Titletype titleType) {
		Map map = Tools.transBean2Map(titleType);
		List<Titletype> list = titleTypeService.findByCondition(map);
		JSONObject json = new JSONObject();
		json.put("record", list);
		return json.getString("record");
	}
}
